<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <link rel="dns-prefetch" href="https://zjg555543.gitee.io">
  
  <title>站内搜索 | Hexo</title>
  <meta name="author" content="John Doe">
  
  
  <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">

  <meta property="og:title" content="站内搜索"/>
  <meta property="og:site_name" content="Hexo"/>

  
    <meta property="og:image" content=""/>
  

  <link rel="alternate" href="/zjg555543.gitee.io/atom.xml" title="Hexo" type="application/atom+xml">

  <link rel="preload" as="style" href="/zjg555543.gitee.io/css/style.css" media="screen" type="text/css">
  <link rel="stylesheet" href="/zjg555543.gitee.io/css/style.css" media="screen" type="text/css">

  <link rel="icon" class="js-site-favicon" type="image/svg+xml" href="https://github.githubassets.com/favicons/favicon.svg">
  <link href="https://unpkg.com" rel="dns-prefetch" />
  <link href="https://busuanzi.ibruce.info" rel="dns-prefetch" />
  <link href="https://cdn1.lncld.net" rel="dns-prefetch" />
  
<meta name="generator" content="Hexo 5.4.0"></head>

<body>
  <div class="container">
    <div class="left-col" style="background-image:url('https://tva1.sinaimg.cn/large/007S8ZIlgy1ggdmi6yapmj30dw0zk0wm.jpg')">
      <div class="intrude-less">
        <header id="header" class="inner">
          <a href="/zjg555543.gitee.io/">
            <div class="profilepic"><img src='https://tva1.sinaimg.cn/large/007S8ZIlgy1ggdmr1k3k0j305k05k74j.jpg'></div>
          </a>
          <div class="author-name"><a href="/zjg555543.gitee.io/">John Doe</a></div>
          
          <nav id="main-nav">
            <ul class="main">
              
              <li>
                
                  <a href="/zjg555543.gitee.io/archives">归档</a>
                
              </li>
              
              <li>
                
                  <a href="/zjg555543.gitee.io/categories">专题</a>
                
              </li>
              
              <li>
                
                  <a href="/zjg555543.gitee.io/search">搜索</a>
                
              </li>
              
            </ul>
          </nav>
          <nav id="sub-nav">
            <div class="social">
              
              
              <a class="weibo" target="_blank" rel="noopener" href="https://weibo.com/zjg555543" title="微博">微博</a>
              
              
              <a class="github" target="_blank" rel="noopener" href="https://github.com/zjg555543" title="Github">Github</a>
              
              

              
            </div>
          </nav>
        </header>
      </div>
    </div>
    <div class="mid-col">
      <div class="mid-col-container">
        <div id="content" class="inner">
          <article>
  <h1 class="title">站内搜索 </h1>
  <div class="entry-content wrapper">
    <div id="search">
      <input type="text" placeholder="请输入关键字" id="Jinput">
    </div>
    <div class="info">
      本次搜索结果共<span id="Jcount">0</span>条
    </div>
    <div id="JresList"></div>

  


    <script>
      let searchData = [{"title":"新的博客新的开始","content":"新的开始找来找去，博客还是落脚在这吧。搭建过wordpress，费尽心思的排除各种问题和疑难杂症，为了不用备案，买了海外的服务器。文章也是不是偶尔更新下，持续写了十来年。2020年，服务器到期，然后心学来潮，加上工作太忙，没有续费。十年来的网站就全部不见了，还好之前自己备份过一部分网站数据，尚未恢复。\n太阳总会升起，一切都是新的开始","url":"/2021/10/12/新的博客新的开始/","link":"","plink":"https://zjg555543.gitee.io/2021/10/12/%E6%96%B0%E7%9A%84%E5%8D%9A%E5%AE%A2%E6%96%B0%E7%9A%84%E5%BC%80%E5%A7%8B/"}]

      function getIndexByWord(word, text, caseSensitive) {
        var wordLen = word.length;

        if (wordLen === 0) {
          return [];
        }

        var startPosition = 0,
          position = [],
          index = [];

        if (!caseSensitive) {
          text = text.toLowerCase();
          word = word.toLowerCase();
        }

        while ( (position = text.indexOf(word, startPosition) ) > -1) {
          index.push({
            position: position,
            word: word
          });

          startPosition = position + wordLen;
        }
        return index;
      }

      function highlightKeyword(text, slice) {
        var result = '';
        var prevEnd = slice.start;
        slice.hits.forEach(function (hit) {
          result += text.substring(prevEnd, hit.position);
          var end = hit.position + hit.length;
          result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
          prevEnd = end;
        });
        result += text.substring(prevEnd, slice.end);
        return result;
      }

      function searchFunc(search_id, content_id) {

        var DB = searchData;
        var input = document.getElementById(search_id);
        var resultContent = document.getElementById(content_id);

        var inputEventFunction = function () {
          var searchText = input.value.trim().toLowerCase();

          if(!searchText){
            return;
          }

          var keywords = searchText.split(/[\s\-]+/);
          if (keywords.length > 1) {
            keywords.push(searchText);
          }

          var resultItems = [];

          if (searchText.length > 0) {

            DB.forEach(function (item) {
              var isMatch = false;
              var hitCount = 0;
              var searchTextCount = 0;
              var title = item.title ? item.title.trim() : '';
              var titleInLowerCase = title.toLowerCase();
              var content = item.content ? item.content.trim().replace(/<[^>]+>/g, "") : '';
              var contentInLowerCase = content.toLowerCase();
              var articleUrl = decodeURIComponent(item.url);

              var indexOfTitle = [];
              var indexOfContent = [];

              keywords.forEach(function (keyword) {
                indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
              });


              if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                isMatch = true;
                hitCount = indexOfTitle.length + indexOfContent.length;
              }

              if (isMatch) {

                [indexOfTitle, indexOfContent].forEach(function (index) {
                  index.sort(function (itemLeft, itemRight) {
                    if (itemRight.position !== itemLeft.position) {
                      return itemRight.position - itemLeft.position;
                    } else {
                      return itemLeft.word.length - itemRight.word.length;
                    }
                  });
                });

                function mergeIntoSlice(text, start, end, index) {
                  var item = index[index.length - 1];
                  var position = item.position;
                  var word = item.word;
                  var hits = [];
                  var searchTextCountInSlice = 0;
                  while (position + word.length <= end && index.length != 0) {
                    if (word === searchText) {
                      searchTextCountInSlice++;
                    }
                    hits.push({ position: position, length: word.length });
                    var wordEnd = position + word.length;

                    index.pop();
                    while (index.length != 0) {
                      item = index[index.length - 1];
                      position = item.position;
                      word = item.word;
                      if (wordEnd > position) {
                        index.pop();
                      } else {
                        break;
                      }
                    }
                  }
                  searchTextCount += searchTextCountInSlice;
                  return {
                    hits: hits,
                    start: start,
                    end: end,
                    searchTextCount: searchTextCountInSlice
                  };
                }

                var slicesOfTitle = [];
                if (indexOfTitle.length != 0) {
                  slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                }

                var slicesOfContent = [];
                while (indexOfContent.length != 0) {
                  var item = indexOfContent[indexOfContent.length - 1];
                  var position = item.position;
                  var word = item.word;

                  var start = position - 20;
                  var end = position + 80;
                  if (start < 0) {
                    start = 0;
                  }
                  if (end < position + word.length) {
                    end = position + word.length;
                  }
                  if (end > content.length) {
                    end = content.length;
                  }
                  slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                }

                slicesOfContent.sort(function (sliceLeft, sliceRight) {
                  if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                    return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                  } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                    return sliceRight.hits.length - sliceLeft.hits.length;
                  } else {
                    return sliceLeft.start - sliceRight.start;
                  }
                });

                slicesOfContent = slicesOfContent.slice(0, 2);

                var resultItem =  "<li class='item'><a target='_blank' href='" + articleUrl + "' class='search-result-title'>" + (slicesOfTitle.length? highlightKeyword(title, slicesOfTitle[0]) : title)+ "</a>";

                slicesOfContent.forEach(function (slice) {
                  resultItem += "<a target='_blank' class='search-result-link' href='" + articleUrl + "'>" +
                    "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                    "...</p>" + "</a>";
                });

                resultItem += "</li>";
                resultItems.push({
                  item: resultItem,
                  searchTextCount: searchTextCount,
                  hitCount: hitCount,
                  id: resultItems.length
                });
              }
            })
            Jcount.innerHTML = resultItems.length;
          };

          if (keywords.length === 1 && keywords[0] === "") {
            resultContent.innerHTML = '<div id="no-result"></div>'
          } else if (resultItems.length === 0) {
            resultContent.innerHTML = '<div id="no-result"></div>'
          } else {
            resultItems.sort(function (resultLeft, resultRight) {
              if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                return resultRight.searchTextCount - resultLeft.searchTextCount;
              } else if (resultLeft.hitCount !== resultRight.hitCount) {
                return resultRight.hitCount - resultLeft.hitCount;
              } else {
                return resultRight.id - resultLeft.id;
              }
            });
            var searchResultList = '<ul class=\"search-result-list\">';
            resultItems.forEach(function (result) {
              searchResultList += result.item;
            })
            searchResultList += "</ul>";
            resultContent.innerHTML = searchResultList;
          }
        }

        input.addEventListener('input', inputEventFunction);
        input.addEventListener('keydown', (e)=>{
          if (e.keyCode=='13') {
            inputEventFunction()
          }
        });
      }
      searchFunc('Jinput', 'JresList')
    </script>
  </div>
</article>
        </div>
        
        
      </div>
      <footer id="footer" class="inner">
        © 2021 - John Doe -
        <span id="busuanzi_container_site_pv">PV <span id="busuanzi_value_site_pv"></span></span>
        <p>Powered by <a target="_blank" rel="noopener" href="https://hexo.io/">Hexo</a> & <a target="_blank" rel="noopener" href="https://github.com/thinkerchan/hexo-theme-greyshade">GreyShade</a></p>
      </footer>
    </div>
  </div>
  
  <script async defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
  
  <script async defer src="https://s5.cnzz.com/z_stat.php?id=1276809529&web_id=1276809529"></script>
  
</body>
</html>